package cn.xfdzx.android.apps.shop.activity.myshop;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Parcelable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.SelectImageAdapter;
import cn.xfdzx.android.apps.shop.adapter.SelectTypeAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.SelectImageBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ItemLengthListener;
import cn.xfdzx.android.apps.shop.contract.SelectImageContract;
import cn.xfdzx.android.apps.shop.presenter.SelectImagePresenter;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

public class SelectImageActivity extends BaseMvpActivity<SelectImagePresenter> implements SelectImageContract.View, ItemLengthListener {

    @BindView(R.id.ll_state)
    LinearLayout llState;

    @BindView(R.id.rv_view)
    RecyclerView rvView;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.tv_upload)
    TextView tvUpload;
    @BindView(R.id.tv_one_type)
    TextView tvOneType;
    @BindView(R.id.tv_one_type_line)
    TextView tvOneTypeLine;
    @BindView(R.id.tv_two_type)
    TextView tvTwoType;
    @BindView(R.id.tv_two_type_line)
    TextView tvTwoTypeLine;
    @BindView(R.id.tv_three_type)
    TextView tvThreeType;
    @BindView(R.id.tv_three_type_line)
    TextView tvThreeTypeLine;
    private List<CategoryAllBeen.DataBean> typeData;
    private SelectTypeAdapter selectTypeAdapter;
    private SelectImageAdapter selectImageAdapter;
    private List<String> oneList;

    private int one_position = 0;
    private int two_position = 0;
    private RelativeLayout.LayoutParams item_bar;
    private boolean is_tuku = false;
    private String type = "";
    public static int GOOD_IS_SELECT = -1;//是否已经选择
    private ArrayList<String> imageUrl = new ArrayList<>();
    private SelectImageBeen bean;


    @Override
    public int getLayoutId() {
        return R.layout.activity_select_type;
    }

    @Override
    public void initView() {
        initHigh();
        mPresenter = new SelectImagePresenter();
        mPresenter.attachView(this);
        oneList = new ArrayList<>();
        typeData = (List<CategoryAllBeen.DataBean>) getIntent().getSerializableExtra("data");
        one_position = getIntent().getIntExtra("one_position", -1);
        two_position = getIntent().getIntExtra("two_position", -1);
        type = getIntent().getStringExtra("type");
        is_tuku = getIntent().getBooleanExtra("tuku", false);


        rvView.setLayoutManager(new LinearLayoutManager(this));
        rvView.setItemAnimator(new DefaultItemAnimator());
        selectTypeAdapter = new SelectTypeAdapter(this, oneList);

        if (one_position != -1) {
            setTextColor(3);
            tvTwoType.setText(typeData.get(one_position).getName());
            oneList.clear();
            for (CategoryAllBeen.DataBean.ChildBean dataBean : typeData.get(one_position).getChild()) {

                oneList.add(dataBean.getName());
                selectTypeAdapter.notifyDataSetChanged();
            }
        } else {
            setDefult();
            oneList.clear();
            for (CategoryAllBeen.DataBean dataBean : typeData) {
                oneList.add(dataBean.getName());
            }
        }

        selectTypeAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (one_position == -1) {
                    one_position = position;
                    tvTwoType.setText(typeData.get(position).getName());
                    setTextColor(2);
                    oneList.clear();
                    for (CategoryAllBeen.DataBean.ChildBean dataBean : typeData.get(position).getChild()) {

                        oneList.add(dataBean.getName());
                        selectTypeAdapter.notifyDataSetChanged();
                    }
                } else {
                    two_position = position;
                    setTextColor(3);
                    if (!is_tuku) {
                        Intent intent = new Intent();
                        intent.putExtra("one_position", one_position);
                        intent.putExtra("two_position", two_position);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
            }
        });
        rvView.setAdapter(selectTypeAdapter);

        selectImageAdapter = new SelectImageAdapter(this, type, this, imageUrl);

        rvImage.setLayoutManager(new LinearLayoutManager(this));
        rvImage.setItemAnimator(new DefaultItemAnimator());
        rvImage.setAdapter(selectImageAdapter);
    }


    public void setTextColor(int p) {
        switch (p) {
            case 1:
                rvImage.setVisibility(View.GONE);
                tvUpload.setVisibility(View.GONE);
                rvView.setVisibility(View.VISIBLE);
                if (type.equals("0")) {
                    tvOneType.setTextColor(getResources().getColor(R.color.color_FB9129));
                } else {
                    tvOneType.setTextColor(getResources().getColor(R.color.color_02C557));
                }
                tvOneType.setVisibility(View.VISIBLE);
                tvOneTypeLine.setVisibility(View.VISIBLE);
                tvTwoType.setVisibility(View.GONE);
                tvTwoType.setTextColor(getResources().getColor(R.color.color_666666));
                tvTwoTypeLine.setVisibility(View.GONE);
                tvThreeType.setVisibility(View.GONE);
                tvThreeType.setTextColor(getResources().getColor(R.color.color_666666));
                tvThreeTypeLine.setVisibility(View.GONE);
                break;
            case 2:
                rvImage.setVisibility(View.GONE);
                tvUpload.setVisibility(View.GONE);
                rvView.setVisibility(View.VISIBLE);
                tvOneType.setTextColor(getResources().getColor(R.color.color_666666));
                tvOneTypeLine.setVisibility(View.GONE);
                tvTwoType.setVisibility(View.VISIBLE);
                if (type.equals("0")) {
                    tvTwoType.setTextColor(getResources().getColor(R.color.color_FB9129));
                } else {
                    tvTwoType.setTextColor(getResources().getColor(R.color.color_02C557));
                }
                tvTwoTypeLine.setVisibility(View.VISIBLE);

                tvThreeType.setVisibility(View.GONE);
                tvThreeType.setTextColor(getResources().getColor(R.color.color_666666));
                tvThreeTypeLine.setVisibility(View.GONE);
                break;
            case 3:
                tvOneType.setTextColor(getResources().getColor(R.color.color_666666));
                tvOneTypeLine.setVisibility(View.GONE);
                if (is_tuku) {
                    rvImage.setVisibility(View.VISIBLE);
                    tvUpload.setVisibility(View.VISIBLE);
                    rvView.setVisibility(View.GONE);
                    tvTwoType.setVisibility(View.VISIBLE);
                    tvTwoType.setTextColor(getResources().getColor(R.color.color_666666));
                    tvTwoTypeLine.setVisibility(View.GONE);
                    tvThreeType.setVisibility(View.VISIBLE);
                    if (type.equals("0")) {
                        tvThreeType.setTextColor(getResources().getColor(R.color.color_FB9129));
                    } else {
                        tvThreeType.setTextColor(getResources().getColor(R.color.color_02C557));
                    }
                    tvThreeTypeLine.setVisibility(View.VISIBLE);
                    mPresenter.selectImage(typeData.get(one_position).getChild().get(two_position).getId() + "");
                } else {
                    tvUpload.setVisibility(View.GONE);
                    tvTwoType.setVisibility(View.VISIBLE);
                    if (type.equals("0")) {
                        tvTwoType.setTextColor(getResources().getColor(R.color.color_FB9129));
                    } else {
                        tvTwoType.setTextColor(getResources().getColor(R.color.color_02C557));
                    }
                    tvTwoTypeLine.setVisibility(View.VISIBLE);

                    tvThreeType.setVisibility(View.GONE);
                    tvThreeType.setTextColor(getResources().getColor(R.color.color_666666));
                    tvThreeTypeLine.setVisibility(View.GONE);
                }
                break;
        }
    }

    private void setDefult() {
        tvTwoType.setVisibility(View.GONE);
        tvTwoTypeLine.setVisibility(View.GONE);
        tvThreeType.setVisibility(View.GONE);
        tvThreeTypeLine.setVisibility(View.GONE);
        tvUpload.setVisibility(View.GONE);

        if (type.equals("0")) {
            tvOneTypeLine.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            tvTwoTypeLine.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            tvThreeTypeLine.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            tvOneType.setTextColor(getResources().getColor(R.color.color_FB9129));
        } else {
            tvOneTypeLine.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            tvTwoTypeLine.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            tvThreeTypeLine.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            tvOneType.setTextColor(getResources().getColor(R.color.color_02C557));
        }


    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onError(Throwable throwable) {
        Toast.makeText(this, "网络请求错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSelectImage(SelectImageBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
              this.bean=bean;
            selectImageAdapter.setData(bean.getData());

        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_one_type, R.id.tv_two_type, R.id.tv_upload})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_one_type:
                setTextColor(1);
                rvImage.setVisibility(View.GONE);
                tvUpload.setVisibility(View.GONE);
                oneList.clear();
                for (CategoryAllBeen.DataBean dataBean : typeData) {
                    oneList.add(dataBean.getName());
                }
                selectTypeAdapter.notifyDataSetChanged();
                one_position = -1;
                break;
            case R.id.tv_two_type:
                setTextColor(2);
                rvImage.setVisibility(View.GONE);
                tvUpload.setVisibility(View.GONE);
                oneList.clear();
                for (CategoryAllBeen.DataBean.ChildBean dataBean : typeData.get(one_position).getChild()) {
                    oneList.add(dataBean.getName());
                }
                selectTypeAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_upload:
                if (GOOD_IS_SELECT != -1) {
                    Intent intent = new Intent();
                    intent.putExtra("one_position", one_position);
                    intent.putExtra("two_position", two_position);
                    intent.putExtra("stringList", imageUrl);
                    intent.putExtra("goodName", bean.getData().get(GOOD_IS_SELECT).getName());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
                }
                break;

        }

    }

    @Override
    protected void onDestroy() {
        oneList.clear();
        oneList = null;
        imageUrl.clear();
        imageUrl = null;
        GOOD_IS_SELECT=-1;
        super.onDestroy();
    }

    @Override
    public void onItemClick() {
        if (imageUrl.size() == 0) {
            tvUpload.setText("上传");
            tvUpload.setBackgroundResource(R.drawable.d999999_bg_r19);
        } else {
            tvUpload.setText("上传(" + imageUrl.size() + ")");
            if (type.equals("0")) {
                tvUpload.setBackgroundResource(R.drawable.dfb9129_bg_r19);
            } else {
                tvUpload.setBackgroundResource(R.drawable.d02c557_bg_r19);
            }
        }
    }
}
